package server.data;

import util.*;
import java.io.*;
import java.util.Random;

/**
 *
 * @author Danon
 */
public class DataManager {
    
    private DataManager() {}
    
    public void init() {
        loadData();
//        String[] files = new String[g.getVertices().size()];
//        g.getVertices().toArray(files);
//        Random rand = new Random(System.currentTimeMillis());
//        for(int i=0; i<10000; i++) {
//            g.addEdge(files[rand.nextInt(files.length)], files[rand.nextInt(files.length)], new Link<Long>(i, "link1", 1L));
//        }
    }
    
    public DirectedGraph<String, Link<Long>> getGraph() {
        return g;
    }
    
    public static DataManager getInstance() {
        if(instance == null)
            instance = new DataManager();
        return instance;
    }
    
    private static void loadData() {
        File dataFile = new File(DEFAULT_DATA_FILE);
        if(dataFile.canRead()) {
            if(loadFromFile())
                return;
        }
        if(g==null) {
            g = new DirectedGraph<String, Link<Long>>();
            System.out.println("Loading file system tree...");
            load(new File("C:\\"));
            System.out.println("Loaded tree for C:");
            load(new File("D:\\"));
            System.out.println("Loaded tree for D:");
            load(new File("E:\\"));
            System.out.println("Loaded tree for E:");
            System.out.println("Writing to data-file...");
            ObjectOutputStream os = null;
            try{
                os = new ObjectOutputStream(new FileOutputStream(dataFile));
                os.writeObject(g);
            } catch (Exception ex) {
                System.out.println("Error occured while writing data to file.");
            }
            try {
                if(os!=null) os.close();
            } catch (IOException ex) {
                System.out.println("Failed to close output data-file.");
            }
        }
        System.out.println("Loading completed.");
    }
    
    private static boolean loadFromFile() {
        ObjectInputStream in  = null;      
        System.out.println("Trying to load data from data-file...");
        try{
            in = new ObjectInputStream(new FileInputStream(DEFAULT_DATA_FILE));
            g = (DirectedGraph<String, Link<Long>>)in.readObject();
        } catch (Exception ex) {
            System.out.println("Failed to load data-file");
            g = null;
        }
        try {
            if(in!=null) in.close();
        } catch (IOException ex) {}
        return g!=null;
    }

    private static void load(File root) {
        //g.addVertex(root.getAbsolutePath());
        if(!root.isDirectory()) return;
        String[] f = root.list();
        if(f==null) return;
        for(int i=0; i<f.length; i++) {
            File file = new File(root, f[i]);
            Link<Long> link = new Link<Long>(i, "Link", 1L);
            g.addEdge(root.getAbsolutePath(), file.getAbsolutePath(), link);
            g.addEdge(file.getAbsolutePath(), root.getAbsolutePath(), link);
            load(file);
        }
    }
    
    private static DataManager instance = null;
    private static DirectedGraph<String, Link<Long>> g;
    
    public static final String DEFAULT_DATA_FILE = "graph";
}
